package com.freejava.testmongodb.controller;

import com.freejava.testmongodb.dao.StockDao;
import com.freejava.testmongodb.entity.Stock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
public class StockController {

    @Autowired
    StockDao stockDao;

    @GetMapping("/test")
    public void test() {
        List<Stock> stocks = new ArrayList<>();
        Stock s1 = new Stock();
        s1.setId(1);
        s1.setCode("002001");
        s1.setName("JavaString");
        s1.setCurrentPrice("31.53");
        s1.setST(false);
        s1.setUp_time(System.currentTimeMillis() / 1000);
        stocks.add(s1);

        Stock s2 = new Stock();
        s2.setId(2);
        s2.setCode("002002");
        s2.setName("avMongoDB");
        s2.setCurrentPrice("202.08");
        s2.setST(false);
        s2.setUp_time(System.currentTimeMillis() / 1000);
        stocks.add(s2);

        System.out.println(stocks);

        // 保存数据到Mongodb，并且会自动创建collection
        // stockDao.insert(stocks);

        // 删除
        // stockDao.deleteById(2);

        // 修改
        // Stock stock = stockDao.findById(1).get();
        // stock.setName("JavaBigDecimal");
        // stock.setCurrentPrice("66.88");
        // stockDao.save(stock);

        // where name = ?
        Stock result = stockDao.findByNameEquals("JavaString");
        System.out.println(result);

        // where name like '%av%'
        List<Stock> resultList = stockDao.findByNameContains("av");
        System.out.println(resultList);

        // where code = ?
        Stock result2 = stockDao.findByCodeEquals("002002");
        System.out.println(result2);
    }
}
